{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from utils import DicomImage, Image\n",
    "from sklearn.cluster import KMeans\n",
    "import cv2\n",
    "import os\n",
    "import shutil"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "feature_cols = [\"f{}\".format(i) for i in range(4)]\n",
    "cols = [\"index\", \"start_height\", \"start_width\", \"height\", \"width\"] + \\\n",
    "        feature_cols + [\"filepath\"] \n",
    "\n",
    "df = pd.read_csv(\"data.csv\", header=-1, names=cols)\n",
    "df = df[df.f0.notnull() & df.f1.notnull() & df.f2.notnull() & df.f3.notnull()]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Kmeans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2     58841\n",
      "13    32058\n",
      "12    28962\n",
      "22    23130\n",
      "9     21415\n",
      "29    20726\n",
      "5     19271\n",
      "16    17448\n",
      "4     17317\n",
      "1     16455\n",
      "27    16129\n",
      "25    16023\n",
      "20    15068\n",
      "17    14573\n",
      "18    14389\n",
      "14    12324\n",
      "15    12073\n",
      "3     10153\n",
      "11     9178\n",
      "10     8492\n",
      "24     7993\n",
      "28     7917\n",
      "6      7336\n",
      "7      6566\n",
      "21     5930\n",
      "26     5063\n",
      "8      4842\n",
      "19     3358\n",
      "0      2724\n",
      "23     2322\n",
      "Name: label, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "kmeans = KMeans(n_clusters=30, random_state=0).fit(df[feature_cols].values)\n",
    "labels = pd.Series(kmeans.labels_, name=\"label\")\n",
    "print(labels.value_counts())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Merge & Sample"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df[\"label\"] = kmeans.labels_\n",
    "\n",
    "sample_df = df[[\"index\", \"label\", \"filepath\", \"start_height\", \"start_width\", \"height\", \"width\"]]\\\n",
    "    .sample(frac=0.01)\n",
    "\n",
    "assert sample_df.notnull().all().all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "if os.path.exists(\"samples/\"):\n",
    "    shutil.rmtree(\"samples/\")\n",
    "\n",
    "for row in sample_df.itertuples():\n",
    "    img = DicomImage(row.filepath)\n",
    "    bw_img = img.getBWImg()\n",
    "    block = bw_img[row.start_height: row.start_height + row.height, \n",
    "                     row.start_width: row.start_width + row.width]\n",
    "    folder = \"samples/{}/\".format(row.label)\n",
    "    file_to_save = \"samples/{}/{}.jpg\".format(row.label, row.index)\n",
    "    if os.path.exists(folder) is False:\n",
    "        os.makedirs(folder) \n",
    "    cv2.imwrite(file_to_save, block)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"samples/labels.txt\", \"w\") as f:\n",
    "    f.write(str(labels.value_counts()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
